# -*- coding: utf-8 -*-

# from sec_downloader import Downloader

# dl = Downloader("MyCompanyName", "email@example.com")

# peers = ["AAPL","MSFT","GOOGL","AMZN","META"]
# for t in peers:
#     dl.get_filing_metadatas("10-K", t, after="2020-01-01", before="2023-12-31")

# metadatas = dl.get_filing_metadatas("AAPL/0000320193-23-000077")
# print(metadatas)

# metadatas = dl.get_filing_metadatas("aapl/000032019323000077")
# metadatas = dl.get_filing_metadatas("320193/000032019323000077")
# metadatas = dl.get_filing_metadatas("320193/0000320193-23-000077")
# metadatas = dl.get_filing_metadatas("0000320193/0000320193-23-000077")
# metadatas = dl.get_filing_metadatas(CompanyAndAccessionNumber(ticker_or_cik="320193", accession_number="0000320193-23-000077"))

# metadatas = dl.get_filing_metadatas(
#     "https://www.sec.gov/ix?doc=/Archives/edgar/data/0001067983/000119312523272204/d564412d8k.htm"
# )
# print(metadatas)

# metadatas = dl.get_filing_metadatas("https://www.sec.gov/Archives/edgar/data/1067983/000119312523272204/d564412d8k.htm")

# from sec_downloader.types import RequestedFilings

# metadatas = dl.get_filing_metadatas(
#     RequestedFilings(ticker_or_cik="MSFT", form_type="10-K", limit=2)
# )
# print(metadatas)

# metadatas = dl.get_filing_metadatas("2/msft/10-K")
# metadatas = dl.get_filing_metadatas("2/789019/10-K")
# metadatas = dl.get_filing_metadatas("2/0000789019/10-K")

# metadatas = dl.get_filing_metadatas("nflx")
# metadatas = dl.get_filing_metadatas("1/NFLX")
# metadatas = dl.get_filing_metadatas("NFLX/10-Q")
# metadatas = dl.get_filing_metadatas("1/NFLX/10-Q")
# metadatas = dl.get_filing_metadatas(RequestedFilings(ticker_or_cik="NFLX"))
# metadatas = dl.get_filing_metadatas(RequestedFilings(limit=1, ticker_or_cik="NFLX", form_type="10-Q"))

# for metadata in metadatas:
#     html = dl.download_filing(url=metadata.primary_doc_url).decode()
#     print(html[:50])
#     break  # same for all filings, let's just print the first one


import asyncio, aiohttp, os, re, datetime as dt

BASE = "https://www.sec.gov/Archives/edgar/daily-index/form-idx"
HEADERS = {"User-Agent": "your@email.com"}

async def download(session, ticker, year):
    url = f"{BASE}/{year}/QTR4/form.idx"
    async with session.get(url, headers=HEADERS) as r:
        txt = await r.text()
    for line in txt.splitlines():
        if line.startswith(ticker) and "10-K" in line:
            cols = line.split()
            cik, name, form, date, path = cols[1], cols[2], cols[0], cols[3], cols[4]
            fname = f"{ticker}_10K_{year}.htm"
            async with session.get(path, headers=HEADERS) as f:
                open(f"data/{fname}", "wb").write(await f.read())
            break

async def main(tickers, years):
    os.makedirs("data", exist_ok=True)
    async with aiohttp.ClientSession() as session:
        tasks = [download(session, t, y) for t in tickers for y in years]
        await asyncio.gather(*tasks)

tickers = ["AAPL","MSFT","NVDA","GOOGL"]
asyncio.run(main(tickers, range(2020, 2024)))


